Skip to content

[perf] canvas: isolate published project reads and prevent history GIL starvation during canvas open and switch - #562

Merged
FayeSpica merged 3 commits into
ThinkFlowLab:mainfrom
FayeSpica:perf/canvas-read-isolation-and-history
Nov 29, 2026
Merged

FayeSpica merged 3 commits into
ThinkFlowLab:mainfrom
FayeSpica:perf/canvas-read-isolation-and-history

Conversation

@FayeSpica

Copy link
Copy Markdown
Collaborator

Summary

Opening or switching between large projects with long commit histories previously hit 12 s timeouts and 409 storage_busy errors. This PR keeps published project reads isolated from unrelated catalog-lock writers, stops background history endpoints from starving the Python GIL, and defers non-essential canvas startup reads.

  • Serve published project snapshots and lightweight navigation listings (GET /api/creation/projects?navigation=true) without holding the global catalog lock across unrelated reads, while preserving editing leases, locks, and atomic publication.
  • Defer canvas text bodies (GET /api/creation/projects/{id}/canvas-texts) and reference evidence checks (GET /api/creation/projects/{id}/reference-evidence) until after initial canvas structure renders, and skip the redundant model_copy(deep=True) on read-only POST /open-canvas responses.
  • Switch read-only history scans (reference_catalog, _inventory, asset_list, _usage_many, versions) from history() deep copies to shared immutable records(), and resolve the project directory once per reference_catalog call with a single snapshot_media_reader.
  • Raise RECORD_CACHE_BYTES from 512 MiB to 1 GiB to stop LRU thrashing on 500+ MB commit chains, parse records directly with CommitRecord.model_validate_json, coalesce concurrent cold _read_history calls per (path, head) (joining in-flight warms), and yield the GIL briefly during uncached history walks so Uvicorn keeps serving foreground requests.
  • Load useProjectMentionAssets lazily when a node composer opens instead of on every canvas open or switch, and throttle reference-catalog through BACKGROUND_PROJECT_READ.

Linked issue

Relates to #153.

  • Fixes #N or Refs #N is included, or no issue applies

Labels

  • Apply type:bug (or type:perf) and area:canvas

Test plan

  • Backend history, storage, and API tests: pytest -q tests/test_creation_history_service.py tests/test_creation_storage.py tests/test_creation_node_versions.py tests/test_creation_api.py
  • Frontend workspace and client tests: node --test tests/backendWorkspace.test.ts tests/creationClient.test.ts
  • Production build and bilingual check: npm run build && npm run check:i18n
  • Isolated browser latency check against a 239-project local workspace (166-node source project and 71-node / 697-commit / 516 MB target project, production bundle, isolated Uvicorn server on localhost):
    • Cold open: 5 fresh browser contexts navigating directly to the 166-node project, waiting for canvas readiness and full background HTTP drain.
    • Warm switch: 1 browser tab with AI chat collapsed, 2 warm-up switches followed by 10 alternating switches between the 166-node and 71-node projects, measuring click-to-first-feedback and click-to-usable-ready as defined in docs/development/ui-performance.md.

Test result

  • Backend history, storage, and API tests: 152 passed (including assertions in test_reference_catalog_checks_files_against_its_one_snapshot that reference_catalog performs zero history() deep copies and resolves _published_manifest once across multiple assets).
  • Frontend workspace and client tests: 62 passed.
  • Production build and bilingual check: npm run build passed; npm run check:i18n passed with 3,413 keys and 0 problems.
  • Isolated browser latency check (end-to-end click/navigation to ready; 12 s timeout budget; before this PR, 2/10 cold opens and 3/8 switches timed out at ≥ 12.00 s, and warm switches failed to drain 4 background endpoints within 60.00 s):
    • Cold open (5/5 succeeded, 0 timeouts, reference-catalog not requested on load): median 0.82 s, P90 0.92 s (range 0.81 s–0.92 s).
    • Warm switch (12/12 drained cleanly, 0 timeouts, 0 409 responses; 10 measured switches): first feedback median 0.02 s, P90 0.03 s (budget P90 ≤ 0.10 s); usable ready median 0.51 s, P90 0.63 s (166-node: 0.44 s–0.50 s, 71-node: 0.51 s–0.63 s; budget median ≤ 0.50 s, P90 ≤ 1.00 s).

Remaining gaps before marking ready for review: warm-switch usable-ready median (0.51 s) is 0.01 s above the 0.50 s reference target on this 697-commit fixture; full CI and nightly browser lanes have not yet run on this branch.

Evidence that it works

Verified by the unit/service regressions and isolated Playwright Chromium latency runs recorded in Test result above.

  • Media attached, or N/A because: performance and read-isolation change with no visual layout modifications.

Provider verification

All tests and browser checks ran against local project fixtures without calling any model provider.

  • Real-provider calls are described honestly, or none were made

Bilingual UI

No user-facing copy changed; npm run check:i18n passed with 0 problems.

  • Both locales are updated, or no user-facing copy changed

…L starvation

Signed-off-by: Weiming Liao <liaowm5@gmail.com>
@FayeSpica FayeSpica added type:bug Something is broken area:canvas Canvas, nodes and direct manipulation labels Nov 29, 2026
… deadlock

Signed-off-by: Weiming Liao <liaowm5@gmail.com>
@FayeSpica
FayeSpica marked this pull request as ready for review November 29, 2026 01:06
## summary
Resolve tool task queries from the published project head and cover
polling while a background commit holds the publication lock.
@FayeSpica
FayeSpica merged commit 9dc6743 into ThinkFlowLab:main Nov 29, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:canvas Canvas, nodes and direct manipulation type:bug Something is broken

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants